package net.ghsoft.Ctrl
{
	import mx.controls.CalendarLayout;
	import mx.controls.Image;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.FlexEvent;
	
	import spark.components.Group;
	import spark.components.VGroup;
	import spark.events.IndexChangeEvent;
	
	[Event(name="change", type="mx.events.CalendarLayoutChangeEvent")]
	
	public class MyDateChoose extends VGroup
	{
		public function MyDateChoose()
		{
			addEventListener(FlexEvent.CREATION_COMPLETE, group_creationCompleteHandler);
			super();
		}
		
		private var _MySelectBox:MySelectBox = null;
		
		private var _CalendarLayout:CalendarLayout = null;
		
		//选择日期后的日期
		private var _NowSelectDate:Number = 0;

		protected function group_creationCompleteHandler(event:FlexEvent):void
		{
			horizontalAlign="center";
			
			
			var gs:Group = new Group();

			var ims:Image = new Image();
			
			[Embed(source="assets/Icon/DateSmall.png")]
			var ois:Class;
			
			ims.source = ois;
			ims.setStyle("fillMode", "repeat");
			ims.horizontalCenter = 0;
			ims.top = 3;
			ims.width = 120
			ims.height = 30;
			gs.addElement(ims);
			
			_MySelectBox = new MySelectBox();
			_MySelectBox.top = 9;
			_MySelectBox.horizontalCenter = 0;
			_MySelectBox.width = 100;
			_MySelectBox.addEventListener("change", MySelectBox_changeHandler);
			gs.addElement(_MySelectBox)
			
			addElement(gs);
			
			var g:Group = new Group();
//			g.bottom = 0; 
//			g.width = 200;
//			g.height = 160;
			addElement(g);

			var i:Image = new Image();
			
			[Embed(source="assets/Icon/Date.png")]
			var oi:Class;
			
			i.source = oi;
			g.addElement(i);
			
			_CalendarLayout = new CalendarLayout();
			_CalendarLayout.horizontalCenter = 0;
			_CalendarLayout.y = 5;
			_CalendarLayout.addEventListener("change", datechooser1_changeHandler);
//			_DateChooser.verticalCenter = 0;
			g.addElement(_CalendarLayout);
			
			var nTemp:Date = new Date();
			_NowSelectDate = nTemp.date;
			callLater(SetMyDate, [nTemp]);
		}
		
		private var _Date1:Date;
		private var _Date2:Date;
		private var _Date3:Date;
		
		private function SetMyDate(dNow:Date):void
		{
			if(_MySelectBox != null)
			{
				if(_NowSelectDate > 28)
				{
					_NowSelectDate = 28;
				}
				_Date1 = new Date(dNow.fullYear, dNow.month - 1, _NowSelectDate);
				
				var strTemp:String = "";
				strTemp += _Date1.fullYear.toString() + "/" + (_Date1.month + 1).toString() + "|";
				
				_Date2 = new Date(dNow.fullYear, dNow.month, _NowSelectDate);
				strTemp += _Date2.fullYear.toString() + "/" + (_Date2.month + 1).toString() + "|";
				_CalendarLayout.selectedDate = _Date2;
				_NowSelectDate = _Date2.date;
			
				_Date3 = new Date(dNow.fullYear, dNow.month + 1, _NowSelectDate);
				strTemp += _Date3.fullYear.toString() + "/" + (_Date3.month + 1).toString();
				
				_MySelectBox.selectDataValue = strTemp;
				callLater(SetMyIndex);
				
				function SetMyIndex():void
				{					
					_MySelectBox.selectedIndex = 1;
				}
			}
		}
		
		protected function MySelectBox_changeHandler(event:IndexChangeEvent):void
		{
			// TODO Auto-generated method stub
			if(event.newIndex > event.oldIndex)
			{
				SetMyDate(_Date3);
			}
			else
			{
				SetMyDate(_Date1);
			}
			
			dispatchEvent(new CalendarLayoutChangeEvent("change", false, false, _CalendarLayout.selectedDate));
		}
		
		protected function datechooser1_changeHandler(event:CalendarLayoutChangeEvent):void
		{
			// TODO Auto-generated method stub
			_NowSelectDate = _CalendarLayout.selectedDate.date;
			dispatchEvent(new CalendarLayoutChangeEvent("change", false, false, _CalendarLayout.selectedDate));
		}
		
		public function get selectedDate():Date
		{
			return _CalendarLayout.selectedDate;
		}
	}
}